package com.bang.sgg.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Auther: Bang
 * @Date: 2019/11/25 22:35
 * @Description:
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).
                withUser("zhangsan").password("123456").roles("VIP1","VIP2").and()
                .withUser("lisi").password("123456").roles("VIP2","VIP3").and()
                .withUser("wangwu").password("123456").roles("VIP1","VIP2","VIP3");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll().
                antMatchers("/level1/**").hasRole("VIP1").
                antMatchers("/level2/**").hasRole("VIP2").
                antMatchers("/level3/**").hasRole("VIP3").
        anyRequest().authenticated().and().
        formLogin().loginPage("/userlogin").loginProcessingUrl("/login").permitAll().and().
        logout().logoutSuccessUrl("/userlogin").logoutUrl("/logout").permitAll().and().
        rememberMe().rememberMeParameter("rememberMe");
        //disable csrf
        http.csrf().disable();

    }


    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

}
